package com.symria.client.admin;

import java.util.List;

import com.symria.framework.page.ATTPage;
import com.symria.framework.page.Page;
import com.symria.framework.page.PagerFactory;
import com.symria.framework.util.BaseAction;
import com.symria.framework.util.Config;
import com.symria.framework.util.FrameCache;
import com.symria.framework.util.StringUtil;
import com.symria.server.bo.system.AdminService;
import com.symria.server.po.CustomInfo;
import com.symria.server.po.MenuInfo;
import com.symria.server.po.SysUser;
import com.symria.server.po.UserInfo;
import com.symria.system.constants.ATTSystem;
import com.symria.system.constants.BaseAttribute;
import com.symria.system.util.SessionHelper;

public class AdminAction extends BaseAction {
  
	private Integer userId;
	private AdminService adminService;
	private UserInfo u;
	private String status;
	private CustomInfo c;
	private Integer id;
	
	public CustomInfo getC() {
		return c;
	}
	public void setC(CustomInfo c) {
		this.c = c;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	public Integer getUserId() {
		return userId;
	}
	public void setUserId(Integer userId) {
		this.userId = userId;
	}
	public AdminService getAdminService() {
		return adminService;
	}
	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}
	public UserInfo getU() {
		return u;
	}
	public void setU(UserInfo u) {
		this.u = u;
	}
	public String home(){
		SysUser user = SessionHelper.getSysUser();
		List<MenuInfo> list = FrameCache.getInstance().getMenuCacheByRole(user.getRoleInfo().getRoleId());
		setAttr(BaseAttribute.LIST, list);
		return SUCCESS;
	}
	public void reloadConfig(){
		Config.tag=false;
	}
	public String toChannel(){
		UserInfo user = null;
		if(!StringUtil.isEmpty(userId)){
			user=adminService.getObject(userId, UserInfo.class);
		}
		setAttr("u", user);
		return SUCCESS;
	}
	public String suChannel(){
		if(null!=u){
			UserInfo user = adminService.getObject(userId, UserInfo.class);
			if(null!=user){
				adminService.updateUserInfo(user, u);
			}else if(!adminService.haveUser(u.getUserName())){
				adminService.saveUserInfo(u);
			}else{
				addFieldError("fromError", "用户名["+u.getUserName()+"]已经存在!");
				setAttr("error", "error");
			}
		}
		return hasFieldErrors()?ERROR:SUCCESS;
	}
	public String searchChannel(){
		Page page =  PagerFactory.createPage();
		adminService.searchChannel(u, page);
		setAttr(ATTPage.PAGE_LIST, page);
		return SUCCESS;
	}
	public String updateStatus(){
		if(!StringUtil.isEmpty(userId) && !StringUtil.isEmpty(status)){
			UserInfo user = adminService.getObject(userId, UserInfo.class);
			if(null!=user){
				user.setStatus(ATTSystem.LOGIC_FALSE.equals(status)?ATTSystem.LOGIC_FALSE:ATTSystem.LOGIC_TRUE);
				adminService.updateObject(user);
			}
		}
		return SUCCESS;
	}
	public String toCustom(){
		setAttr("c", adminService.getObject(id, CustomInfo.class));
		return SUCCESS;
	}
	public String suCustom(){
		CustomInfo oc = adminService.getObject(id, CustomInfo.class);
		if(oc!=null){
			adminService.updateCustom(oc, c);
		}else{
			adminService.saveObject(c);
		}
		return SUCCESS;
	}
	public String searchCustom(){
		Page page = PagerFactory.createPage();
		adminService.searchCustomInfo(c, page);
		setAttr(ATTPage.PAGE_LIST, page);
		return SUCCESS;
	}
	
}
